package com.dxq.oa.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.dxq.oa.dao.UserDao;
import com.dxq.oa.entity.Menu;
import com.dxq.oa.entity.Role;
import com.dxq.oa.entity.User;
import com.dxq.oa.service.UserService;
import com.dxq.oa.utils.FormatData;
import com.dxq.oa.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public User login(User user) {
        User user1 = this.userDao.selectByName(user.getUseraccount());
        if(user1==null){
            throw new RuntimeException("用户名不存在");
        }
        if(!DigestUtils.md5DigestAsHex(user.getPassword().getBytes()).equals(DigestUtils.md5DigestAsHex(user1.getPassword().getBytes()))){
            throw new RuntimeException("密码错误");
        }
        String s = this.getClass().getResource("").toString().split("oa-admin")[0];
        String s1 = s.split("file:/")[1];
        System.out.println("瞎写");
        System.out.println(s1);
        user1.setPhoto(s1+user1.getPhoto());
        return user1;
    }

    @Override
    public List<User> findAll() {
        return this.userDao.selAll();
    }

    @Override
    public List<User> findByPage(String start, String row) {
        int start1 = (Integer.parseInt(start) -1 )*Integer.parseInt(row);
        return this.userDao.findBypage(start1,Integer.parseInt(row));
    }

    @Override
    public long findTotol() {
        return this.userDao.findTotal();
    }

    @Override
    public List<Menu> findMenu() {
        List<Menu> menus = this.userDao.selMenu();
        //循环得到所有根节点
        List<Menu> footer = menus.stream().filter(a -> a.getParentid().equals("0")).collect(Collectors.toList());
        footer.forEach(a-> System.out.println(a));
        //循环得到跟以外的其他节点
        List<Menu> other = menus.stream().filter(a -> !a.getParentid().equals("0")).collect(Collectors.toList());
        other.forEach(a-> System.out.println(a));
        //得到转换后的数据进行返回
        for(Menu menu : footer){
            FormatData.getFormatData(menu,other);
        }
        footer.forEach(s-> System.out.println(s));
        return footer;
    }

    @Override
    public boolean addUser(User user) {

        user.setPassword(DigestUtils.md5DigestAsHex("111111".getBytes()));
        int r = this.userDao.insertUser(user);
        if(r == 1){
            return true;
        }
        return false;

    }

    @Override
    public List<Menu> findMenuList() {
        List<Menu> list = this.userDao.selMList();
        return list;
    }

    @Override
    public void alterPwd(String pwd,String token) {

        String oldPwd = pwd.split(",")[1];
        log.info("原始密码："+oldPwd);
        String pwd1 = JWTUtils.getInfo(token).getClaim("pwd").asString();


        if(!oldPwd.equals(pwd1)){
            throw new RuntimeException("原始密码不对，请重新输入");
        }

        this.userDao.updatePwd(pwd.split(",")[0],JWTUtils.getInfo(token).getClaim("account").asString());

    }

    @Override
    public List<Role> getRole() {
        return this.userDao.selRole();
    }
}
